//-----------------------------------------------------------------------
// <copyright file="FriendDAOBase.cs" company="Pyramid Consulting">
//     Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using BirthdayReminder.Entities;

using Pyco;
using Pyco.Diagnostics;
using Pyco.Data;

namespace BirthdayReminder.Data
{    
    public abstract class FriendDAOBase
    {
        #region Common methods
        public virtual Friend CreateFriendFromReader(IDataReader reader)
        {
            Friend item = new Friend();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("UserId"))) item.UserId = (int)reader["UserId"];
                if (!reader.IsDBNull(reader.GetOrdinal("FriendId"))) item.FriendId = (int)reader["FriendId"];
                if (!reader.IsDBNull(reader.GetOrdinal("Allow"))) item.Allow = (bool)reader["Allow"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessCreateFriendFromReaderException, ex);
            }
            return item;
        }
        #endregion
		
        #region CreateFriend methods
            
        public virtual void CreateFriend(Friend friend)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spFriendsInsert");
                
                database.AddInParameter(dbCommand, "@UserId", DbType.Int32, friend.UserId);
                database.AddInParameter(dbCommand, "@FriendId", DbType.Int32, friend.FriendId);
                database.AddInParameter(dbCommand, "@Allow", DbType.Boolean, friend.Allow);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessCreateFriendException, ex);
            }
        }

        #endregion
		
        #region UpdateFriend methods
        
        public virtual void UpdateFriend(Friend friend)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spFriendsUpdate");            
                
                database.AddInParameter(dbCommand, "@UserId", DbType.Int32, friend.UserId);
                database.AddInParameter(dbCommand, "@FriendId", DbType.Int32, friend.FriendId);
                database.AddInParameter(dbCommand, "@Allow", DbType.Boolean, friend.Allow);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessUpdateFriendException, ex);
            }
        }
        
        #endregion
		
        #region DeleteFriend methods
        public virtual void DeleteFriend(int userId, int friendId)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spFriendsDelete");
                
                database.AddInParameter(dbCommand, "@UserId", DbType.Int32, userId);
                database.AddInParameter(dbCommand, "@FriendId", DbType.Int32, friendId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessDeleteFriendException, ex);
            }
        }

        #endregion
		
        #region GetFriend methods
        
        public virtual Friend GetFriend(int userId, int friendId)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spFriendsGet");
                
                database.AddInParameter(dbCommand, "@UserId", DbType.Int32, userId);
                database.AddInParameter(dbCommand, "@FriendId", DbType.Int32, friendId);
                
                Friend friend =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        friend = CreateFriendFromReader(reader);
                        reader.Close();
                    }
                }
                return friend;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessGetFriendException, ex);
            }
        }
        
        #endregion
		
        #region GetFriendList methods
        public virtual FriendCollection GetFriendList(FriendColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spFriendsGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                FriendCollection friendCollection = new FriendCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        Friend friend = CreateFriendFromReader(reader);
                        friendCollection.Add(friend);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return friendCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessGetFriendListException, ex);
            }
        }
        
        public virtual FriendCollection GetFriendList(FriendColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetFriendList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
    }
}